//
//  SettingViewController.m
//  Router
//
//  Created by teki on 15/10/13.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "SettingViewController.h"
#import "SystemSettingTableViewCell.h"

#import "UnbindViewController.h"
#import "AboutViewController.h"

#import "SystemSettingHelper.h"
#import "DiscoverNewWIFIController.h"
#import "UpgradeView.h"

@interface SettingViewController ()<UIAlertViewDelegate>
@property (nonatomic, strong) SystemSettingHelper *systemHelper;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"系统设置";
    
    
    self.systemHelper = [[SystemSettingHelper alloc] init];
//    [self setTableViewStyle:UITableViewStyleGrouped];

    [self.tableView registerClass:[SystemSettingTableViewCell class] forCellReuseIdentifier:@"SystemSettingTableViewCell"];
    self.tableView.separatorColor = SeparatorLineColor;
    self.tableView.tableFooterView = [UIView new];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)findNewWIFINotification:(NSNotification *)notify
{
    BOOL hasNewWIFI = [[notify object] boolValue];
    if (hasNewWIFI) {
        [self addLeftBarButtonWithNormalImageName:@"home_location" selectedImageName:@"home_location_pre" target:self action:@selector(leftButtonPressed:)];
    }
    self.navigationItem.leftBarButtonItem.customView.hidden = !hasNewWIFI;
}


- (void)leftButtonPressed:(id)sender
{
    DiscoverNewWIFIController *discoverNewWIFI = [[DiscoverNewWIFIController alloc] init];
    discoverNewWIFI.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:discoverNewWIFI animated:YES];

}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TK_FindNewWIFINotification object:nil];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(findNewWIFINotification:) name:TK_FindNewWIFINotification object:nil];
}


#pragma mark -
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SystemSettingTableViewCell *cell = (SystemSettingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SystemSettingTableViewCell"];
    [cell congfigCellWith:nil andIndexPath:indexPath];
    return cell;
}



#pragma mark -
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        [self pushViewControllerWithClassName:NSStringFromClass([UnbindViewController class])];
    }else if (indexPath.row == 1)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定重新启动WIFI？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
        
    }
    else if (indexPath.row == 2)
    {
        [self pushViewControllerWithClassName:@"AboutViewController"];

    }else{
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"UpgradeView" owner:nil options:nil];
        UpgradeView *upgradeView = (UpgradeView *)array[0];
        upgradeView.stations = [UserSingleton shareInstance].userModel.stations;
        if ([upgradeView.stations count]> 0) {
            [upgradeView showInView:[UIApplication sharedApplication].keyWindow];
            [upgradeView check];
        }
        else{
            [self.view showToastText:@"暂时没有WIFI"];
        }

    }

}


// 设置分隔线 iOS8 下才起作用
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 18, 0, 18)];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 18, 0, 18)];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 18, 0, 18)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 18, 0, 18)];
    }
}


#pragma mark -
#pragma mark   UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self restartWIFI];
    }
}

#pragma mark -
#pragma mark   IBActions
- (void)restartWIFI
{
    WIFIModel *wifi = [UserSingleton shareInstance].userModel.wifi;
    self.activity = [self startActivityWithTitle:@"正在重启,请稍候..."];
    [self.systemHelper restartDeviceWithDeviceID:wifi.device_id success:^(BOOL succ, id responseObject, NSString *msg) {
        if (responseObject)
        {
            [self dispatchTimerWithTime:5.0 block:^{
                [self stopActivityWithActivity:self.activity title:@""];
            }];
        }
        else{
            [self stopActivityWithActivity:self.activity title:@"重启失败!"];
        }

    } failure:^(NSError *error) {
        NSString *failedReason = [error localizedDescription];
        [self stopActivityWithActivity:self.activity title:failedReason];
    }];
}

@end
